package com.thomasmoerman.dci2.tmc

import org.squeryl.PrimitiveTypeMode._
import com.thomasmoerman.dci2.domain._

trait MoneyTransferQueries {
  
  def calculateBalance(accountId: Long): Double
  
  def persist(moneyTransfer: MoneyTransfer): Unit
  
}

trait MoneyTransferQueriesSqueryl extends MoneyTransferQueries {
  this: Tables =>
  
  def calculateBalance(accountId: Long): Double =
    from(moneyTransfers)(mt =>
      where(mt.accountId === accountId)
      compute(nvl(sum(mt.amount), 0)))
  
  def persist(moneyTransfer: MoneyTransfer): Unit = moneyTransfers.insert(moneyTransfer)
  
}